package com.example.account.controller;


import com.example.account.repository.DataRepository;
import com.example.account.repository.MyoRepository;
import com.example.account.service.DataService;
import com.example.account.service.MyoService;
import com.example.account.util.POIUtils;
import controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ResponseBody
@RestController
@RequestMapping("/excel")
public class MyoController extends BaseController {

    @Autowired
    private MyoRepository myoRepository;

    @Autowired
    private DataRepository dataRepository;

    @Autowired
    private MyoService myoService;


    /**
     * 想要读取Excel就这样写：
     */
    public void importExcel(MultipartFile file) {
        List<ExcelDTO> excelDTOS = readExcel(file, ExcelDTO.class);
        //这个集合对应的就是excel的内容，集合的每一个元素代表excel一行
        System.out.println(excelDTOS);
    }


    @PostMapping("/look")
    public void look(@RequestParam("myo_electricity") MultipartFile excelFile){
        try {
            List<String> list = POIUtils.readExcel(excelFile);
//            list.removeIf(Objects::isNull);去掉null值
            //去掉空字符串
            Iterator<String> iterator = list.iterator();
            while (iterator.hasNext()){
                if (iterator.next() == ""){
                    iterator.remove();
                }
            }
            //遍历list,查看数据
            for (String s : list) {
                System.out.println(s);
            }
            //创建map对象或者pojo类存入所需的数据，
            Map<String,Object> map = new HashMap<>();
            map.put("myo",list.get(0));

            System.out.println(map);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 这个类是每个属性都和excel表头对应
     */
    private class ExcelDTO {

        private String id;

        private String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }


}
